<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/8 0008
 * Time: 上午 10:43
 */
namespace App\Api\Service\Supplier\Controllers;

use Framework\BaseClass\Api\Controller;
use App\Eloquent\Ygt\Product;
use App\Eloquent\Ygt\Supplier;
use App\Eloquent\Ygt\ProductSupplier;
use App\Eloquent\Ygt\DepartmentUser;
use App\Engine\Func;
use App\Eloquent\Province;
use App\Eloquent\City;
use App\Eloquent\Area;

class IndexController extends Controller
{
    //获取某个材料的供应商列表
    public function listOfOneProduct(){
        $productId = request('product_id');
        /*$productModel = new Product();
        $productInfo = $productModel->getOneData(['id'=>$productId]);
        return $productInfo->supplier->toArray();*/

        $productSupplierModel = new ProductSupplier();

        $where = [
            'product_id'=>$productId
        ];

        $fields = 'ygt_supplier.*,ygt_product_supplier.number';

        $page = request('page');
        $limit = 10;
        $offset = ($page - 1) * $limit;

        $join = [
            ['table'=>'ygt_supplier', 'field_l'=>'ygt_supplier.id', 'field_c'=>'=', 'field_r'=>'ygt_product_supplier.supplier_id']
        ];

        $result = $productSupplierModel->getData($where, $fields, $limit, $offset, ['ygt_supplier.sort_id','desc'], '', $join);

        $list = $result->map(function($item, $key){
            $tmp = $this->composeInfo($item);
            $tmp['number'] = $item->number;
            return $tmp;
        })->toArray();

        $count = $productSupplierModel->getCount($where);

        $pagingData = [
            'page' => $page,
            'pageSize' => $limit,
            'total' => $count,
            'keyword' => ''
        ];
        return $this->pagingData($list, $pagingData);

    }

    public function getList(){
        $userId = Func::getHeaderValueByName('userid');
        $userInfo           = DepartmentUser::getCurrentInfo( $userId );
        $companyId = isset($userInfo['company_id']) ? $userInfo['company_id'] : '';

        $supplierModel = new Supplier();
        $where = [
            'company_id'=>$companyId
        ];
        if($supplierName = request('supplier_name')){
            $where['supplier_name'] = ['like','%'.$supplierName.'%'];
        }

        $fields = '*';

        $page = request('page');
        $limit = 10;
        $offset = ($page - 1) * $limit;

        $result = $supplierModel->getData($where, $fields, $limit, $offset);

        $list = $result->map(function($item, $key){
            return $this->composeInfo($item);
        })->toArray();

        $count = $supplierModel->getCount($where);

        $pagingData = [
            'page' => $page,
            'pageSize' => $limit,
            'total' => $count,
            'keyword' => ''
        ];
        return $this->pagingData($list, $pagingData);


    }

    public function getInfo(){
        $where = [
            'id'=>request('id')
        ];
        $rules = [
            'id' => 'required|integer',
        ];
        $this->validateRequestParameters($where, $rules);

        $userId = Func::getHeaderValueByName('userid');
        $userInfo           = DepartmentUser::getCurrentInfo( $userId );
        $companyId = isset($userInfo['company_id']) ? $userInfo['company_id'] : '';
        $where['company_id'] = $companyId;

        $supplierModel = new Supplier();
        if($item = $supplierModel->getOneData($where)){
            $info = $this->composeInfo($item);
            return $info;
        }else{
            xThrow(ERR_CUSTOMER_IS_NOT_EXIST);
        }
    }

    private function composeInfo($item){
        $tmp = [
            'id'=>$item->id,
            'supplier_name'=>$item->supplier_name,
            'supplier_tel'=>$item->supplier_tel,
            'supplier_address'=>$item->supplier_address,
            'avatars_id'=>$item->avatars_id,
            'avatars_url'=>Func::getImgUrlById($item->avatars_id),
            'supplier_level'=>$item->supplier_level,
            'supplier_level_title'=>CustomerEngine::getLevelTitle($item->supplier_level),
            'contact_person'=>$item->contact_person,
            'contact_phone'=>$item->contact_phone,
            'contact_address'=>$item->contact_address,
        ];
        return $tmp;
    }


    public function add(){

        $user_id        = Func::getHeaderValueByName( 'userid' );
        $userInfo           = DepartmentUser::getCurrentInfo( $user_id );
        $companyId = isset($userInfo['company_id']) ? $userInfo['company_id'] : '';

        $params = request()->only('avatars_id', 'supplier_name', 'supplier_tel', 'supplier_address', 'contact_person', 'contact_phone', 'contact_address', 'supplier_level', 'province_id', 'city_id', 'area_id', 'address');

        $params['avatars_id'] = $params['avatars_id']?$params['avatars_id']:0;

        $rules = [
            'supplier_name' => 'required',
            'avatars_id' => 'integer',
        ];
        $this->validateRequestParameters($params, $rules);

        $supplierModel = new Supplier();

        $provinceInfo = Province::where('provinceID',$params['province_id'])->first();
        $cityInfo = City::where('cityID',$params['city_id'])->first();
        $areaInfo = Area::where('areaID',$params['area_id'])->first();
        $createData = [
            'avatars_id' => $params['avatars_id'],
            'supplier_name' => $params['supplier_name'],
            'supplier_tel' => $params['supplier_tel'],
            'supplier_address' => $params['supplier_address'],
            'contact_person' => $params['contact_person'],
            'contact_phone' => $params['contact_phone'],
            'contact_address' => $params['contact_address'],
            'supplier_level' => $params['supplier_level'],
            'province_id' => $params['province_id'],
            'city_id' => $params['city_id'],
            'area_id' => $params['area_id'],
            'province_name' => isset($provinceInfo->province)?$provinceInfo->province:'',
            'city_name' => isset($cityInfo->city)?$cityInfo->city:'',
            'area_name' => isset($areaInfo->area)?$areaInfo->area:'',
            'address' => $params['address'],
            'company_id' => $companyId,
        ];
        $supplierId = $supplierModel->insertOneData($createData,'id');


        $returnData = [
            'msg'=>'ok',
            'supplier_id'=>$supplierId
        ];
        return $returnData;

    }
}